package com.mysoft.gfyx.marketdevelop.mip.agent;

import com.alibaba.fastjson.JSON;
import com.mysoft.framework.cache.api.CacheProvider;
import com.mysoft.framework.modeling.dto.OptionItem;
import com.mysoft.gfyx.marketdevelop.mip.GetDeveloperDataService;
import com.mysoft.gfyx.marketdevelop.mip.GetProjectDataService;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjCustom.CommonPageRequestDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjCustom.LandTagListRequestDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjCustom.ResponseDataDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjCustom.SqlApiResponseDto;
import com.mysoft.gfyx.marketdevelop.service.DeveloperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class GetProjectDataServiceAgent {

    @Autowired
    private GetProjectDataService getProjectDataService;

    @Autowired
    private GetDeveloperDataService getDeveloperDataService;

    @Autowired
    private DeveloperService developerService;

    @Resource
    private CacheProvider cacheProvider;

    /**
     * @param type: 1是默认,2 增加"不限"选项
     * @return
     */
    public List<OptionItem> getTagPrjFunction(int type) {
        String cacheKey = "MARKETDEVELOP:MOHURDPROJECT:PRJFUNCTION" + type;
        List<OptionItem> prjFunctionOption = cacheProvider.get(cacheKey);

        prjFunctionOption = new ArrayList<>();
        if (type == 2) {
            OptionItem item = new OptionItem();
            item.setText("全部");
            item.setValue("全部");
            prjFunctionOption.add(item);
        }
        CommonPageRequestDto commonPageRequestDto = new CommonPageRequestDto();
        commonPageRequestDto.setPageNum(1);
        commonPageRequestDto.setPageSize(100);
        SqlApiResponseDto prjFunction = getProjectDataService.getTagPrjFunction(commonPageRequestDto);
        List<OptionItem> finalPrjFunctionOption = prjFunctionOption;
        prjFunction.getData().getRows().forEach(p -> {
            OptionItem item = new OptionItem();
            item.setText(p.get("name").toString());
            item.setValue(p.get("name").toString());
            if (type == 2) {
                item.setParent("全部");
            }
            finalPrjFunctionOption.add(item);
        });

        cacheProvider.set(cacheKey, finalPrjFunctionOption, 7200);

        return finalPrjFunctionOption;
    }

    /**
     * @param type: 1是默认,2 增加"不限"选项
     * @return
     */
    public List<OptionItem> getTagPrjProperty(int type) {
        String cacheKey = "MARKETDEVELOP:MOHURDPROJECT:PRJPROPERTY" + type;
        List<OptionItem> prjPropertyOption = cacheProvider.get(cacheKey);
        if (prjPropertyOption != null) {
            return prjPropertyOption;
        } else {
            prjPropertyOption = new ArrayList<>();
        }
        if (type == 2) {
            OptionItem item = new OptionItem();
            item.setText("全部");
            item.setValue("全部");
            prjPropertyOption.add(item);
        }
        CommonPageRequestDto commonPageRequestDto = new CommonPageRequestDto();
        commonPageRequestDto.setPageNum(1);
        commonPageRequestDto.setPageSize(100);
        SqlApiResponseDto prjProperty = getProjectDataService.getTagPrjProperty(commonPageRequestDto);
        List<OptionItem> finalPrjPropertyOption = prjPropertyOption;
        prjProperty.getData().getRows().forEach(p -> {
            OptionItem item = new OptionItem();
            item.setText(p.get("name").toString());
            item.setValue(p.get("name").toString());
            if (type == 2) {
                item.setParent("全部");
            }
            finalPrjPropertyOption.add(item);
        });

        cacheProvider.set(cacheKey, finalPrjPropertyOption, 7200);

        return finalPrjPropertyOption;
    }

    /**
     * @param type: 1是默认,2 增加"不限"选项
     * @return List<OptionItem>
     */
    public List<OptionItem> getTagLandType(int type) {
        ResponseDataDto landDataResult = getLandTypeData();
        List<OptionItem> finalLandTypeOption = new ArrayList<>();
        if (landDataResult.getRows().size() < 1) {
            return finalLandTypeOption;
        }
        try {
            landDataResult.getRows().forEach(r -> {
                OptionItem item = new OptionItem();
                String parentValue = r.get("category").toString();
                item.setValue(parentValue);
                item.setText(parentValue);
                finalLandTypeOption.add(item);
                List<String> subItems = JSON.parseArray(r.get("land_usage").toString(), String.class);
                subItems.forEach(t -> {
                    if (!t.equals(parentValue)) {
                        OptionItem subItem = new OptionItem();
                        subItem.setParent(parentValue);
                        subItem.setText(t);
                        subItem.setValue(t);
                        finalLandTypeOption.add(subItem);
                    }
                });
            });
            return finalLandTypeOption;
        } catch (Exception e) {
            return finalLandTypeOption;
        }
    }

    public List<OptionItem> getTagLandTypeCategory() {
        ResponseDataDto landDataResult = getLandTypeData();
        List<OptionItem> finalLandTypeOption = new ArrayList<>();
        if (landDataResult.getRows().size() < 1) {
            return finalLandTypeOption;
        }
        try {
            landDataResult.getRows().forEach(r -> {
                OptionItem item = new OptionItem();
                String parentValue = r.get("category").toString();
                item.setValue(parentValue);
                item.setText(parentValue);
                finalLandTypeOption.add(item);
//                List<String> subItems = JSON.parseArray(r.get("land_usage").toString(), String.class);
//                subItems.forEach(t -> {
//                    OptionItem subItem = new OptionItem();
//                    subItem.setText(t);
//                    subItem.setValue(t);
//                    finalLandTypeOption.add(subItem);
//                });
            });
            return finalLandTypeOption;
        } catch (Exception e) {
            return finalLandTypeOption;
        }
    }

    public List<OptionItem> getTagLandTypeSub(String category) {
        ResponseDataDto landDataResult = getLandTypeData();
        List<OptionItem> finalLandTypeOption = new ArrayList<>();
        if (landDataResult.getRows().size() < 1) {
            return finalLandTypeOption;
        }
        try {
            landDataResult.getRows().forEach(r -> {
                List<String> subItems = JSON.parseArray(r.get("land_usage").toString(), String.class);
                subItems.forEach(t -> {
                    if (Objects.equals(category, "") || Objects.equals(category, r.get("category").toString())) {
                        OptionItem subItem = new OptionItem();
                        subItem.setText(t);
                        subItem.setValue(t);
                        subItem.setParent(r.get("category").toString());
                        finalLandTypeOption.add(subItem);
                    }
                });

            });
            return finalLandTypeOption;
        } catch (Exception e) {
            return finalLandTypeOption;
        }
    }

    public List<String> getLandTypeByCategory(String category) {
        List<String> res = new ArrayList<>();
        if (Objects.equals(category, "")) {
            return res;
        }

        ResponseDataDto landDataResult = getLandTypeData();
        if (landDataResult.getRows().size() < 1) {
            return res;
        }

        try {
            landDataResult.getRows().forEach(r -> {
                if (Objects.equals(r.get("category").toString(), category)) {
                    List<String> subItems = JSON.parseArray(r.get("land_usage").toString(), String.class);
                    res.addAll(subItems);
                }
            });
            return res;
        } catch (Exception e) {
            return res;
        }
    }

    public ResponseDataDto getLandTypeData() {
        String cacheKey = "MARKETDEVELOP:TAKENLAND:LANDTYPE";
        ResponseDataDto res = cacheProvider.get(cacheKey);
        if (res != null) {
            return res;
        }
        LandTagListRequestDto requestDto = new LandTagListRequestDto();
        requestDto.setPageNum(1);
        requestDto.setPageSize(1000);
        SqlApiResponseDto result = getDeveloperDataService.landTagList(requestDto);
        res = developerService.getSqlApiRes(result);
        cacheProvider.set(cacheKey, res, 3600);
        return res;
    }


}
